package zozopo.attendance;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import zozopo.attendance.operations.LeaveOS;
import zozopo.attendance.operations.LeaveTB;

import zozopo.background.operator.operations.DepartmentOS;
import zozopo.background.operator.operations.OperatorTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class AppLeaveAction  extends ActionSupport implements Action,SessionAware{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	//传入参数
	private int id;
	
	private LeaveTB leave;
	
	private Map<Object, Object> session;
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	public String execute(){
		
		return SUCCESS;
	}

	public void validate() {
		//权限处理
	
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
	
		if(operator == null){
			addFieldError("errormessage",getText("operator.timeoutornotlogin"));
			return ; 
		} 
		
		leave = new LeaveTB();
		
		LeaveOS leaveOS = new LeaveOS();
		DepartmentOS departmentOS = new DepartmentOS(leaveOS.getConn());
		
		leave = leaveOS.get(id);
		
		
		
		if(!departmentOS.isSuperior(operator.getDepartmentid_operator(),leave.getDepartmentid_leave())){
			leaveOS.releaseConnection();
			addFieldError("errormessage","不是上级无法批准假期!");
			return ; 
		}
		
		if(leave.isIsapp_leave()){
			leaveOS.releaseConnection();
			addFieldError("errormessage","已经批过不能再批准!");
			return ; 
		}
		
		leave.setAppfirstname_leave(operator.getFirstname_operator());
		leave.setApplastname_leave(operator.getLastname_operator());
		leave.setAppname_leave(operator.getName_operator());
		leave.setAppoperatorid_leave(operator.getId_operator());
		
		leaveOS.approve(leave);
		
		leave = leaveOS.get(id);//重取验证
		
		leaveOS.releaseConnection();
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public LeaveTB getLeave() {
		return leave;
	}
	public void setLeave(LeaveTB leave) {
		this.leave = leave;
	}
	

}
